package com.rabbitmq.ChattingRoom.controller;

import com.rabbitmq.ChattingRoom.controller.biz.RoomBiz;
import com.rabbitmq.ChattingRoom.util.common.base.Result;
import com.rabbitmq.ChattingRoom.util.common.req.*;
import com.rabbitmq.ChattingRoom.util.common.resp.RoomRESP;
import com.rabbitmq.ChattingRoom.util.common.resp.UserRESP;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "room",produces = "application/json;charts=utf-8")
public class RoomController {

    @Autowired
    private RoomBiz roomBiz;

    /**
     * 用户创建聊天室
     */
    @ApiOperation(value = "创建聊天室",notes = "用户创建聊天室")
    @PostMapping("create")
    public Result<String> createRoom(@RequestBody @Valid CreateChattingRoomReq req, @RequestHeader("TOKEN") String token){
        return roomBiz.createRoom(req,token);
    }

    /**
     * 列出用户加入的聊天室列表
     */
    @ApiOperation(value = "用户加入的聊天室列表", notes = "列出用户加入的聊天室列表")
    @PostMapping("user/rooms")
    public Result<List<RoomRESP>> listRoom(@RequestHeader("TOKEN") String token){
        return roomBiz.listRoom(token);
    }

    /**
     * 用户加入聊天室
     */
    @ApiOperation(value = "用户加入聊天室", notes = "用户加入聊天室接口")
    @PostMapping("user/addRoom")
    public Result<String> joinRoom(@RequestBody @Valid UserJoinRoomReq req){
        return roomBiz.joinRoom(req);
    }

    /**
     * 用户退出聊天室
     */
    @ApiOperation(value = "用户退出聊天室", notes = "用户退出聊天室接口")
    @PostMapping("user/quitRoom")
    public Result<String> exitRoom(@RequestBody @Valid UserJoinRoomReq req,@RequestHeader("TOKEN") String token){
        return roomBiz.exitRoom(req,token);
    }

    /**
     * 列出所有聊天室
     */
    @ApiOperation(value = "列出所有聊天室", notes = "列出当前系统中所有（正常）的聊天室")
    @PostMapping(value = "rooms")
    public Result<List<RoomRESP>> rooms() {
        return roomBiz.rooms();
    }

    /**
     * 删除聊天室
     */
    @ApiOperation(value = "删除聊天室", notes = "删除聊天室")
    @PostMapping("del")
    public Result<String> del(@RequestBody @Valid RoomDelReq req,@RequestHeader("TOKEN") String token) {
        return roomBiz.del(req,token);
    }

    /**
     * 获取未读消息
     */
    @ApiOperation(value = "获取未读消息", notes = "获取未读消息")
    @PostMapping("getMsg")
    public Result<List<HistoryMSGReq>> getMsgByCount(@RequestBody GetMsgReq req) {
        return roomBiz.getMsgByCount(req);
    }

    /**
     * 获取当前聊天室中的用户列表
     */
    @ApiOperation(value = "获取当前聊天室中的用户列表", notes = "获取当前聊天室中的用户列表")
    @PostMapping("getUsers/{roomId}")
    public Result<List<UserRESP>> getUsers(@PathVariable("roomId") Long roomId) {
        return roomBiz.getUsers(roomId);
    }
}
